home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / cc.zip / CC.C next >
Text File  |  1986-09-18  |  3KB  |  86 lines

  1. /****************************************************************
  2. *                                                               *
  3. *                       CC (C Checker)                          *
  4. *                                                               *
  5. *            C Source Paren, bracket and comment Checker        *
  6. *                                                               *
  7. *                T. Jennings  -- Sometime in 1983               *
  8. *                                                               *
  9. *      only slightly rewritten for the C86 C compiler           *                                               *
  10. *                                                               *
  11. *            by Claude Ciari 9-18-1986                          *                         *
  12. *                                                               *
  13. ****************************************************************/
  14.  
  15.  
  16.  
  17. #include <stdio.h>
  18. #include <ctype.h>
  19.  
  20. /* Very crude but very effective C source debugger. Counts the numbers of
  21. matching braces, parenthesis and comments, and displays them at the left edge
  22. of the screen. The best way to see what it does is to do it;*/
  23.  
  24.     /* CC CC.C        /* C Check CC.C */
  25.  
  26.  
  27. main(argc,argv)
  28. int argc;
  29. char **argv;
  30. {
  31. int file;    
  32. char c,lastc;
  33. int parens,brackets,comments;
  34. int line, col;
  35. char hdr[40];
  36.  
  37.     file= open(argv[1],0x8000);
  38.     if (file == -1) {
  39.         printf("File missing. Try CCC <filename.ext> \r\n");
  40.         exit();
  41.     }
  42.     brackets= parens= comments= 0;
  43.     line= 0; col= 0;
  44.     lastc= '\0';
  45.  
  46.     while (read(file,&c,1)) {
  47.         if (c == 0x1a) break;            /* stop if ^Z */
  48.  
  49.         if (col == 0) {
  50.             sprintf(hdr,"%d: {%d} (%d) /*%d*/",line,brackets,parens,comments);
  51.             while (strlen(hdr) < 23)    /* gross but works */
  52.                 strcat(hdr," ");    /* to hell with elegant, */
  53.             printf("%s|",hdr);        /* we got a job to do !*/
  54.         }                /* (real programmers dont ...) */
  55.  
  56. /* Don't count parens and brackets that are inside comments. This of course
  57. assumes that comments are properly matched; in any case, that will be the
  58. first thing to look for. */
  59.  
  60.         if (comments <= 0) {
  61.             if (c == '{') ++brackets;
  62.             if (c == '(') ++parens;
  63.             if (c == '}') --brackets;
  64.             if (c == ')') --parens;
  65.         }
  66.  
  67. /* Now do comments. This properly handles nested comments, whether or
  68. not the compiler does is your responsibility */
  69.  
  70.         if ((c == '*') && (lastc == '/')) ++comments;
  71.         if ((c == '/') && (lastc == '*')) --comments;
  72.  
  73.         ++col;
  74.         if (c == 0x0a) {        /* newline == New Line */
  75.             col= 0;            /* set column 0 */
  76.             ++line;
  77.         }
  78.         putchar(c);            /* display text */
  79.         lastc= c;            /* update last char */
  80.     }
  81.     printf("\r\n\r\n");
  82.     if (brackets) printf("Unbalanced brackets\r\n");
  83.     if (parens) printf("Unbalanced parenthesis\r\n");
  84.     if (comments) printf("Unbalanced comments\r\n");
  85. }
  86.